<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="80%" destroy-on-close @closed="$emit('closed')" align-center="true" :close-on-click-modal="false">
	<el-container>
        <el-aside width="200px">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="group" class="menu" node-key="id" :data="groupData" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
            <el-header>
                <div>
                    <el-input v-model="search.keywords" placeholder="请输入关键词" clearable></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch">查询</el-button>
                </div>
            </el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @select-all="selectAll" @selection-change="selectionChange" stripe remoteSort remoteFilter border height="600" :params="query">
					<el-table-column type="selection"  align="center" width="50"></el-table-column>
					<el-table-column type="index"  align="center" width="50"></el-table-column>
					<el-table-column label="设备编码" prop="code" align="center"></el-table-column>
					<el-table-column label="设备名称" prop="title" align="center"></el-table-column>
					<el-table-column label="设备规格" prop="spec" align="center"></el-table-column>
					<el-table-column label="资产类别" prop="asset_class_title" align="center"></el-table-column>
					<el-table-column label="设备类别" prop="class_title" align="center"></el-table-column>
					<el-table-column label="设备状态" prop="state_title" align="center"></el-table-column>
					<el-table-column label="使用部门" prop="dep_title" align="center"></el-table-column>
					<el-table-column label="报修单位" prop="dep_title" align="center"></el-table-column>
					<el-table-column label="供应商" prop="apply_title" align="center"></el-table-column>
					<el-table-column label="父设备" prop="apply_title" align="center"></el-table-column>
					<el-table-column label="备注" prop="remark" align="center"></el-table-column>
				</scTable>
			</el-main>
            <el-footer>
                <div style="display: flex;justify-content: end;">
                    <el-button @click="visible = false">取 消</el-button>
                    <el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
                </div>
            </el-footer>
		</el-container>
	</el-container>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
    props:{
        query: { type: Object, default: () => ({}) },
    },
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '关联父设备',
				edit: '维修转单',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			groupFilterText: '',
            groupData: [
                { id: "",label: "所有"},
                { id: "1",label: "设备位置",},
                { id: "2",label: "设备类别",},
                { id: "3",label: "组织架构",},
            ],
			ids: "",
			apiObj: this.$API.device.deviceList.getList,
            // query:{
            //     no_ids: "12,11,10,13",
            // },
            selection:[],
            search: {
				keywords: null,
			},
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		async submit() {
            if (this.selection.length == 1) {
				this.isSaveing = true;
				var res = [];
                var reqData = { ids: this.ids,pid:this.selection[0].id }
                res = await this.$API.device.addFather.post(reqData);
				this.isSaveing = false;
				if (res.code == 1) {
					this.$emit('success', this.selection, this.mode)
					this.visible = false;
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", { type: 'error' })
				}
			} else {
				this.$message.error("请选择设备")
			}
			// this.$refs.dialogForm.validate(async (valid) => {
			// 	if (valid) {
			// 		this.isSaveing = true;
			// 		var res = [];
            //         var reqData = { ids: this.tag_ids,pid:this.selection[0].id }
			// 		res = await this.$API.device.addFather.post(reqData);
			// 		this.isSaveing = false;
			// 		if (res.code == 1) {
			// 			this.$emit('success', this.selection, this.mode)
			// 			this.visible = false;
			// 			this.$message.success("关联成功")
			// 		} else {
			// 			this.$alert(res.message, "提示", { type: 'error' })
			// 		}
			// 	} else {
			// 		return false;
			// 	}
			// })
		},
		//表单注入数据
		setData(data) {
			this.ids = data
		},
        upsearch() {
			this.$refs.table.upData(this.search)
		},
        selectAll() {
			this.$message.error("只能单选！")
			this.$refs.table.clearSelection()
		},
        selectionChange(selection) {
			if(selection.length > 1){
				const del_row = selection.shift()
				this.$refs.table.toggleRowSelection(del_row,false) //设置这一行取消选中
			}
			this.selection = selection;
		},
	}
}
</script>

<style></style>
